package cn.devzyh.controller;

import cn.devzyh.domain.PromotionSpace;
import cn.devzyh.domain.ResultVo;
import cn.devzyh.service.PromotionSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告位模块处理器
 */
@RestController
@RequestMapping("/promotionSpace")
public class PromotionSpaceController {

    @Autowired
    private PromotionSpaceService promotionSpaceService;

    // 广告位列表查询
    @GetMapping("/findAllPromotionSpace")
    public ResultVo findAllPromotionSpace() {

        List<PromotionSpace> spaces = promotionSpaceService.findAllPromotionSpace();

        return new ResultVo(true, 200, "查询成功", spaces);
    }

    // 添加&修改广告位
    @PostMapping("/saveOrUpdatePromotionSpace")
    public ResultVo saveOrUpdatePromotionSpace(@RequestBody PromotionSpace space) {

        ResultVo res = new ResultVo(true, 200, "", null);
        if (null == space.getId()) {

            promotionSpaceService.savePromotionSpace(space);
            res.setMessage("广告位新增成功");
        } else {

            promotionSpaceService.updatePromotionSpace(space);
            res.setMessage("广告位修改成功");
        }

        return res;
    }

    // 回显广告位名称
    @GetMapping("/findPromotionSpaceById")
    public ResultVo findPromotionSpaceById(@RequestParam(value = "id", required = true) Integer id) {

        return new ResultVo(true, 200, "响应成功", promotionSpaceService.findOneById(id));
    }
}
